import { api,api1 } from 'boot/axios';
import axios, { AxiosResponse } from 'axios';
import { Respons } from 'components/models';
import { Api } from 'src/store/sphinx/state';
import { useStore } from 'src/store';
import { Loading } from 'quasar';


let loadingTimer;

/**
 * get请求封装
 * @param url
 * @param analyze
 */
export async function getRequest(url: string, analyze?: (data: any) => void, paramData?: any) {
	if (paramData !== undefined && paramData !== null) {
		const params: string[] = [];
		for (const key in paramData) {
			// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/restrict-plus-operands
			params.push(key + '=' + paramData[key]);
		}
		const s = params.join('&');
    let paramUrl: string = ''
    if (url.indexOf('?') !== -1) {
      paramUrl = '&' + s;
    } else {
      paramUrl = '?' + s;
    }

		url += paramUrl;
	}
  await api.get(url).then((res: AxiosResponse<Respons<any>>) => {
		resolveStatus(res, analyze);
	})
}


/**
 * post请求封装
 * @param url
 * @param requestData
 * @param analyze
 */
export function postRequest(url: string, requestData: any, analyze?: (data: any) => void): void {
	void api.post(url, requestData).then((res: AxiosResponse<Respons<any>>) => {
		resolveStatus(res, analyze);
	})
}

//走老向服务器
export function postRequest1(url: string, requestData: any, analyze?: (data: any) => void): void {
  void api1.post(url, requestData).then((res: AxiosResponse<Respons<any>>) => {
    resolveStatus(res, analyze);
  })
}

export async function getRequest1(url: string, analyze?: (data: any) => void, paramData?: any) {
  if (paramData !== undefined && paramData !== null) {
    const params: string[] = [];
    for (const key in paramData) {
      // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/restrict-plus-operands
      params.push(key + '=' + paramData[key]);
    }
    const s = params.join('&');
    let paramUrl: string = ''
    if (url.indexOf('?') !== -1) {
      paramUrl = '&' + s;
    } else {
      paramUrl = '?' + s;
    }

    url += paramUrl;
  }
  await api1.get(url).then((res: AxiosResponse<Respons<any>>) => {
    resolveStatus(res, analyze);
  })
}

/**
 * 对服务器拒绝的处理
 * @param error
 */
function rejectStatus(error) {
	Loading.show({
		message: error
	});
	loadingTimer = setTimeout(() => {
		Loading.hide();
		loadingTimer = void 0;
	}, 1000);
}

/**
 * 对返回状态的处理
 * @param result
 * @param analyze
 */

function resolveStatus(result: AxiosResponse<Respons<any>>, analyze?: (data: any) => void): void {
	const status = result.data.status || result.data.status_code;
	// console.log(result)
	switch (status) {
		case 'SUCCESS' :
			//成功继续业务逻辑获取数据
			if (analyze !== undefined) {
				analyze(result.data.data);
			}
			break;
		case 'FAILD':
			//失败操作
			Loading.show({
				message: result.data.data.message || result.data.data
			});
			loadingTimer = setTimeout(() => {
				Loading.hide();
				loadingTimer = void 0;
				// void router.push('/');
			}, 1000);
			break;
		case 1:
			if (analyze !== undefined) {
				analyze(result.data.body);
			}
			break;
		case 'FAILD':
			;//失败操作
	}
}

/**
 * 通过实体代码获取接口对象
 * @param entityCode
 */
export function getEntityCode(entityCode: string): Api | undefined {
	const api = useStore().state.sphinx.api;
	let apiObj: Api | undefined = undefined;
	api.forEach((x: Api) => {
		if (x.entityCode === entityCode) {
			apiObj = x;
		}
	});
	return apiObj;
}

